package com.dongdongshop.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 运营商登录
 */
@Controller
public class LoginController {

    @RequestMapping({"/","toLogin"})
    public String toLogin(){
        return "login";
    }

    @RequestMapping("login")
    public String login(String userName, String password, Model model){
        //获取subject主体
        Subject subject = SecurityUtils.getSubject();
       //关联Realm 数据库
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userName, password);
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException u){
            //System.out.println("账号错误");
            model.addAttribute("message","账号错误");
            return "login";
        }catch (IncorrectCredentialsException i){
            //System.out.println("密码错误");
            model.addAttribute("meaasge","密码错误");
            return "login";
        }
        return "admin/index";
    }

}
